import xlwt

title_style = xlwt.XFStyle()

title_font = xlwt.Font()
title_font.name = "宋体"
title_font.bold = True
title_font.height = 11 * 20
title_font.colour_index = 0x08
title_style.font = title_font

title_cell_align = xlwt.Alignment()
title_cell_align.horz = 0x02
title_cell_align.vert = 0x01
title_style.alignment = title_cell_align

title_border = xlwt.Borders()
title_border.right = xlwt.Borders.DASHED
title_border.bottom = xlwt.Borders.DOTTED
title_border.top = xlwt.Borders.DOUBLE
title_border.left = xlwt.Borders.HAIR
title_style.borders = title_border

title_pattern = xlwt.Pattern()
title_pattern.pattern = xlwt.Pattern.SOLID_PATTERN
title_pattern.pattern_fore_colour = 22
title_pattern.pattern_back_colour = 11
title_style.pattern = title_pattern

# 创建工作簿
workbook = xlwt.Workbook()

# 创建工作表
worksheet = workbook.add_sheet("Sheet1")

# 填充工作表内容
# 1. 创建合并的汇总标题
worksheet.write_merge(0, 1, 0, 3, "用户汇总", title_style)
# 2. 创建标题

# 写入数据
data = (
    ("序号", "姓名", "年龄", "生日"),
    (1, "海巴图", 11, "01/01/2001"),
    (2, "海巴图", 12, "02/02/2002"),
    (3, "海巴图", 13, "03/03/2003")
)
# enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列，同时列出数据和数据下标，一般用在 for 循环当中。
for i, item in enumerate(data):
    for j, val in enumerate(item):
        # 将数据写入到Excel
        # worksheet.write(i + 2, j, val)
        # 将数据写入到Excel，附带样式
        worksheet.write(i + 2, j, val, title_style)

# 创建sheet
worksheet = workbook.add_sheet("Sheet2")
# 插入图片
worksheet.insert_bitmap("test.bmp", 0, 0)

# 保存文件
workbook.save("用户汇总.xls")
